home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / gtk-2.0 / demo / iconview_edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-25  |  3.8 KB  |  159 lines

  1. /* Icon View/Editing and Drag-and-Drop
  2.  *
  3.  * The GtkIconView widget supports Editing and Drag-and-Drop.
  4.  * This example also demonstrates using the generic GtkCellLayout
  5.  * interface to set up cell renderers in an icon view.
  6.  */
  7.  
  8. #include <gtk/gtk.h>
  9. #include <string.h>
  10. #include "demo-common.h"
  11.  
  12. static GtkWidget *window = NULL;
  13.  
  14. enum
  15. {
  16.   COL_TEXT,
  17.   NUM_COLS
  18. };
  19.  
  20.  
  21. static void
  22. fill_store (GtkListStore *store)
  23. {
  24.   GtkTreeIter iter;
  25.   const gchar *text[] = { "Red", "Green", "Blue", "Yellow" };
  26.   gint i;
  27.  
  28.   /* First clear the store */
  29.   gtk_list_store_clear (store);
  30.  
  31.   for (i = 0; i < 4; i++)
  32.     {
  33.       gtk_list_store_append (store, &iter);
  34.       gtk_list_store_set (store, &iter, COL_TEXT, text[i], -1);
  35.     }
  36. }
  37.  
  38. static GtkListStore *
  39. create_store (void)
  40. {
  41.   GtkListStore *store;
  42.  
  43.   store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING);
  44.  
  45.   return store;
  46. }
  47.  
  48. static void
  49. set_cell_color (GtkCellLayout   *cell_layout,
  50.         GtkCellRenderer *cell,
  51.         GtkTreeModel    *tree_model,
  52.         GtkTreeIter     *iter,
  53.         gpointer         data)
  54. {
  55.   gchar *text;
  56.   GdkColor color;
  57.   guint32 pixel = 0;
  58.   GdkPixbuf *pixbuf;
  59.  
  60.   gtk_tree_model_get (tree_model, iter, COL_TEXT, &text, -1);
  61.   if (gdk_color_parse (text, &color))
  62.     pixel =
  63.       (color.red   >> 8) << 24 |
  64.       (color.green >> 8) << 16 |
  65.       (color.blue  >> 8) << 8;
  66.   g_free (text);
  67.  
  68.   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 24, 24);
  69.   gdk_pixbuf_fill (pixbuf, pixel);
  70.  
  71.   g_object_set (cell, "pixbuf", pixbuf, NULL);
  72.  
  73.   g_object_unref (pixbuf);
  74. }
  75.  
  76. static void
  77. edited (GtkCellRendererText *cell,
  78.     gchar               *path_string,
  79.     gchar               *text,
  80.     gpointer             data)
  81. {
  82.   GtkTreeModel *model;
  83.   GtkTreeIter iter;
  84.   GtkTreePath *path;
  85.  
  86.   model = gtk_icon_view_get_model (GTK_ICON_VIEW (data));
  87.   path = gtk_tree_path_new_from_string (path_string);
  88.  
  89.   gtk_tree_model_get_iter (model, &iter, path);
  90.   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
  91.               COL_TEXT, text, -1);
  92.  
  93.   gtk_tree_path_free (path);
  94. }
  95.  
  96. GtkWidget *
  97. do_iconview_edit (GtkWidget *do_widget)
  98. {
  99.   if (!window)
  100.     {
  101.       GtkWidget *icon_view;
  102.       GtkListStore *store;
  103.       GtkCellRenderer *renderer;
  104.  
  105.       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  106.  
  107.       gtk_window_set_screen (GTK_WINDOW (window),
  108.                  gtk_widget_get_screen (do_widget));
  109.       gtk_window_set_title (GTK_WINDOW (window), "Editing and Drag-and-Drop");
  110.  
  111.       g_signal_connect (window, "destroy",
  112.             G_CALLBACK (gtk_widget_destroyed), &window);
  113.  
  114.       store = create_store ();
  115.       fill_store (store);
  116.  
  117.       icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (store));
  118.       g_object_unref (store);
  119.  
  120.       gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
  121.                     GTK_SELECTION_SINGLE);
  122.       gtk_icon_view_set_orientation (GTK_ICON_VIEW (icon_view),
  123.                      GTK_ORIENTATION_HORIZONTAL);
  124.       gtk_icon_view_set_columns (GTK_ICON_VIEW (icon_view), 2);
  125.       gtk_icon_view_set_reorderable (GTK_ICON_VIEW (icon_view), TRUE);
  126.  
  127.       renderer = gtk_cell_renderer_pixbuf_new ();
  128.       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
  129.                   renderer, TRUE);
  130.       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (icon_view),
  131.                       renderer,
  132.                       set_cell_color,
  133.                       NULL, NULL);
  134.  
  135.       renderer = gtk_cell_renderer_text_new ();
  136.       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view),
  137.                   renderer, TRUE);
  138.       g_object_set (renderer, "editable", TRUE, NULL);
  139.       g_signal_connect (renderer, "edited", G_CALLBACK (edited), icon_view);
  140.       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
  141.                       renderer,
  142.                       "text", COL_TEXT,
  143.                       NULL);
  144.  
  145.       gtk_container_add (GTK_CONTAINER (window), icon_view);
  146.     }
  147.  
  148.   if (!GTK_WIDGET_VISIBLE (window))
  149.     gtk_widget_show_all (window);
  150.   else
  151.     {
  152.       gtk_widget_destroy (window);
  153.       window = NULL;
  154.     }
  155.  
  156.   return window;
  157. }
  158.  
  159.